package com.lxyk.yygh.hosp.controller.api;

import com.lxyk.yygh.common.exception.YyghException;
import com.lxyk.yygh.common.helper.HttpRequestHelper;
import com.lxyk.yygh.common.result.Result;
import com.lxyk.yygh.common.result.ResultCodeEnum;
import com.lxyk.yygh.common.utils.MD5;
import com.lxyk.yygh.hosp.service.DepartmentService;
import com.lxyk.yygh.hosp.service.HospitalService;
import com.lxyk.yygh.hosp.service.HospitalSetService;
import com.lxyk.yygh.hosp.service.ScheduleService;
import com.lxyk.yygh.model.hosp.Department;
import com.lxyk.yygh.model.hosp.Hospital;
import com.lxyk.yygh.model.hosp.Schedule;
import com.lxyk.yygh.vo.hosp.DepartmentQueryVo;
import com.lxyk.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //1.获取传递过来的医院信息
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //2.获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");

        //3.获取医院系统传递过来的签名 sign医院传过来的签名
        String sign = (String) switchMap.get("sign");
        //4.查询数据库的签名  signKey数据库传过来的签名
        String signKey=hospitalSetService.getKye(hoscode);
        //5.加密数据库中的签名 使用MD5摘要算法     encrypt:加密之后的签名
        String encrypt = MD5.encrypt(signKey);

        //医院系统的签名和数据库加密的签名对比
        if(!sign.equals(encrypt))
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);

        //在传输图片过程中 + 转为了""  ,最后要转回来
        String logoData = (String) switchMap.get("logoData");
        if(!StringUtils.isEmpty(logoData)){
            String s = logoData.replaceAll(" ", "+");
            switchMap.put("logoData",s);
        }

        //调用service方法
        hospitalService.save(switchMap);
        return Result.ok();
    }

    @ApiOperation("查询医院")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //1.获取传递过来的医院信息
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //2.获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");

        //3.获取医院系统传递过来的签名 sign医院传过来的签名
        String sign = (String) switchMap.get("sign");
        //4.查询数据库的签名  signKey数据库传过来的签名
        String signKey=hospitalSetService.getKye(hoscode);
        //5.加密数据库中的签名 使用MD5摘要算法     encrypt:加密之后的签名
        String encrypt = MD5.encrypt(signKey);

        //医院系统的签名和数据库加密的签名对比
        if(!sign.equals(encrypt))
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);

        //调用service
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation("上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //1.获取传递过来的医院信息
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //2.获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");

        //3.获取医院系统传递过来的签名 sign医院传过来的签名
        String sign = (String) switchMap.get("sign");
        //4.查询数据库的签名  signKey数据库传过来的签名
        String signKey=hospitalSetService.getKye(hoscode);
        //5.加密数据库中的签名 使用MD5摘要算法     encrypt:加密之后的签名
        String encrypt = MD5.encrypt(signKey);

        //医院系统的签名和数据库加密的签名对比
        if(!sign.equals(encrypt))
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);

        //调用方法
        departmentService.save(switchMap);
        return Result.ok();
    }

    @ApiOperation("查询科室")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //1.获取传递过来的医院信息
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //分页    page:当前页 limit:每页显示条数
        int page=StringUtils.isEmpty(switchMap.get("page"))?1:Integer.parseInt((String) switchMap.get("page"));
        int limit=StringUtils.isEmpty(switchMap.get("limit"))?1:Integer.parseInt((String) switchMap.get("limit"));

        //2.获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");

        //3.获取医院系统传递过来的签名 sign医院传过来的签名
        String sign = (String) switchMap.get("sign");
        //4.查询数据库的签名  signKey数据库传过来的签名
        String signKey=hospitalSetService.getKye(hoscode);
        //5.加密数据库中的签名 使用MD5摘要算法     encrypt:加密之后的签名
        String encrypt = MD5.encrypt(signKey);

        //医院系统的签名和数据库加密的签名对比
        if(!sign.equals(encrypt))
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用方法
        Page<Department> pageDepartment=departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageDepartment);
    }

    @ApiOperation("删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //1.获取传递过来的医院信息
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //2.获取医院的编码&科室编码
        String hoscode = (String) switchMap.get("hoscode");
        String depcode = (String) switchMap.get("depcode");

        //3.获取医院系统传递过来的签名 sign医院传过来的签名
        String sign = (String) switchMap.get("sign");
        //4.查询数据库的签名  signKey数据库传过来的签名
        String signKey=hospitalSetService.getKye(hoscode);
        //5.加密数据库中的签名 使用MD5摘要算法     encrypt:加密之后的签名
        String encrypt = MD5.encrypt(signKey);

        //医院系统的签名和数据库加密的签名对比
        if(!sign.equals(encrypt))
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);

        //调用方法
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    @ApiOperation("上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //1.获取传递过来的医院信息
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //2.获取医院的编码
        String hoscode = (String) switchMap.get("hoscode");

        //3.获取医院系统传递过来的签名 sign医院传过来的签名
        String sign = (String) switchMap.get("sign");
        //4.查询数据库的签名  signKey数据库传过来的签名
        String signKey=hospitalSetService.getKye(hoscode);
        //5.加密数据库中的签名 使用MD5摘要算法     encrypt:加密之后的签名
        String encrypt = MD5.encrypt(signKey);

        //医院系统的签名和数据库加密的签名对比
        if(!sign.equals(encrypt))
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);

        //调用方法
        scheduleService.save(switchMap);
        return Result.ok();
    }

    @ApiOperation(value = "查询排班")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //获取医院编号
        String hoscode = (String) switchMap.get("hoscode");
        //获取科室编号
        String depcode = (String) switchMap.get("depcode");

        //当前页：page 和 每页记录数：limit
        int page = StringUtils.isEmpty(switchMap.get("page"))? 1 : Integer.parseInt((String) switchMap.get("page"));
        int limit = StringUtils.isEmpty(switchMap.get("limit"))? 1 : Integer.parseInt((String) switchMap.get("limit"));

        //TODO 签名校验
        //1 获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) switchMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名 获取签名key
        String signKey = hospitalSetService.getKye(hoscode);

        //3 把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page , limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //获取医院编号
        String hoscode = (String) switchMap.get("hoscode");
        //获取排版编号
        String hosScheduleId = (String) switchMap.get("hosScheduleId");

        //TODO 签名校验
        //1 获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) switchMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名 获取签名key
        String signKey = hospitalSetService.getKye(hoscode);

        //3 把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }
}
